let coord = {
	transitionXY,
	formartXY,
	toDegreeXY,
	toDegree
}

// 度分秒转换为经纬度
function transitionXY(gps){
	let [lon, lat] = gps.split(",");
	let x = formartXY(lon);
	let y = formartXY(lat);
	return {
		x,
		y
	}
};
// 将度分秒格式的经纬度转为84坐标
function formartXY(xy){
	// S为负数N为正，W为负E为正
	let degree = xy.slice(0,xy.indexOf("°")) * 1
	let minute = xy.slice(xy.indexOf("°")+1,xy.indexOf("′")) * 1
	let second = xy.slice(xy.indexOf("′")+1,xy.indexOf("″")) * 1
	let j = xy.slice(xy.indexOf("″")+1, xy.length)
	let lon = degree + minute/60 + second/3600
	if(j == "S" || j == "W")lon = "-"+lon
	return lon*1
};

// 将84坐标转度分秒
function toDegree(xy){
	const angle = Math.abs(xy)
	const deg =parseInt(angle);
	const min = parseInt((angle - deg) * 60);
	const sec = (angle - deg - min / 60) * 3600;
	return `${deg}°${min}′${sec.toFixed(3)}″${xy>0?"+":"-"}`;
};

// 将84坐标转度分秒
function toDegreeXY(gps){
	const {x,y} = gps
	let gpsx = toDegree(x);
	let gpsy = toDegree(y);
	return `${gpsx.replace("+","E").replace("-","W")},${gpsy.replace("+","N").replace("-","S")}`
};

module.exports = coord